home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / jwlthief.zip / FUNWINDO.CLS < prev    next >
Text File  |  1990-04-12  |  7KB  |  256 lines

  1. /* The game. */!!
  2.  
  3. inherit(Window, #FunWindow, #(enemies    /* Bouncing balls */
  4. enemyMove  /* event timer */
  5. field      /* playing limits */
  6. jewels     /* the points */
  7. player     /* Player's position */
  8. playerLife /* remaining lives */
  9. score      /* pts. earned */
  10. scoreBoard /* score display area */
  11. time       /* timer */), 2, nil)!!
  12.  
  13. now(FunWindowClass)!!
  14.  
  15. /* Return the fun window style. */
  16. Def style(self)
  17. { ^WS_OVERLAPPEDWINDOW
  18. }!!
  19.  
  20. /* This space for rent. */
  21. Def sizeRect(self)
  22. {  ^rect(1,171,191,312)
  23. }
  24. !!
  25.  
  26. /* Return the name of this class's MS-Windows 
  27.   window class either for registration or new 
  28.   window creation. */
  29. Def wndIcon(self)
  30. { ^"funicon"
  31. }!!
  32.  
  33. /* Return the name of this class's MS-Windows window class
  34.   either for registration or new window creation. */
  35. Def wndClass(self)
  36. { ^"FunWindow"  }!!
  37.  
  38. /* Create a new window class Struct.
  39.    Change the cursor to a cross.  */
  40. Def newWClass(self, lpCl, lpIcon | wc)
  41. { wc := newWClass(self:WindowClass, lpCl, lpIcon);
  42.   putWord(wc, Call LoadCursor(HInstance, asciiz("funcur")), 14);
  43.   ^wc;
  44. } !!
  45.  
  46. now(FunWindow)!!
  47.  
  48. /* We're really killing then creating */
  49. /*  and returning a new jewel.        */
  50. /**************************************/
  51. Def  moveJewel(self, oldJewel | hDC, newJewel)
  52. {  newJewel := new(Jewel);
  53.    hDC := getContext(self);
  54.    erase(oldJewel, hDC);
  55.    draw(newJewel, hDC, field);
  56.    releaseContext(self, hDC);
  57.    ^newJewel;
  58. }!!
  59.  
  60. /* Display about box. */
  61. /**********************/
  62. Def  WM_SYSCOMMAND(self, wP, lP)
  63. { if wP == IDSABOUT
  64.   then runModal(new(Dialog), ABOUT_BOX, self);
  65.   else ^asInt(execWindowProc(self, #WM_SYSCOMMAND, wP, lP));
  66.   endif;
  67. }!!
  68.  
  69. /* Put the player in a safe position. */
  70. /**************************************/
  71. Def  gotFocus(self, hWndPrev)
  72. { player := point(right(field), top(field));
  73. } !!
  74.  
  75. /* Handle the expiring timer event. */
  76. Def  WM_KILLFOCUS(self, wp, lp)
  77. { player := nil;
  78. }!!
  79.  
  80. /* Return true if  inside client area. */
  81. /***************************************/
  82. Def  inField(self, pos)
  83. { if pos <> nil
  84.   then if  pos.y <= bottom(field)
  85.        and pos.x >= left(field)
  86.        and pos.x <= right(field)
  87.        and pos.y >= top(field)
  88.        then ^true;
  89.        endif;
  90.   else ^false;
  91.   endif;
  92. }!!
  93.  
  94. /* Display the score */
  95. Def  showScore(self | hDC)
  96. { hDC := getContext(self);
  97.   Call TextOut(hDC, right(field)-35, 5,
  98.                asString(score), size(asString(score)));
  99.   releaseContext(self, hDC);
  100. }!!
  101.  
  102. /* Move the guard one speed unit. */
  103. /**********************************/
  104. Def  moveGuards(self | hDC)
  105. { hDC := getContext(self);
  106.    do(enemies,
  107.    {using(guard)
  108.      setGuardDir(self, guard);  /* Check direction */
  109.      move(guard, hDC);          /* Move there */
  110.      if inField(self, player)   /* See if we hit player */
  111.      cand x(player)+7 >= x(guard) - size(guard)
  112.      cand x(player)-7 <= x(guard)
  113.      cand y(player)+7 >= y(guard) - size(guard)
  114.      cand y(player)-7 <= y(guard)
  115.      then playerLife := playerLife - 1;
  116.           if playerLife > 0
  117.           then beep();
  118.           endif;
  119.      endif;
  120.    });
  121.    releaseContext(self, hDC);
  122. }!!
  123.  
  124. /* Bounce off the wall of the window and the scoreBoard. */
  125. /*   Take into account the guard's speed and height so   */
  126. /*   the guard is not half off the screen.               */
  127. /*********************************************************/
  128. Def  setGuardDir(self, guard | guardSize, guardSpeed)
  129. { guardSize := size(guard);
  130.   guardSpeed := speed(guard);
  131.   select
  132.   /* bounce off bottom of window */
  133.     case y(guard) + guardSpeed >= bottom(field)
  134.     is setDir(guard, "N");
  135.     endCase
  136.   /* bounce off top of window, bottom of scoreboard */
  137.     case y(guard) - guardSpeed <= bottom(scoreBoard) + guardSize
  138.     is if x(guard) + guardSpeed >= left(scoreBoard)
  139.        cor y(guard) - guardSpeed <= top(field) + guardSize
  140.           setDir(guard, "S");
  141.        endif;
  142.     endCase
  143.   endSelect;
  144. /* Allow two direction changes per turn */
  145.   select
  146.   /* bounce off left of window */
  147.     case x(guard) - guardSpeed <= left(field) + guardSize
  148.     is setDir(guard, "E");
  149.     endCase
  150.   /* bounce of right of window, left of scoreboard */
  151.     case x(guard) + guardSpeed >= left(scoreBoard)
  152.     is if y(guard) - guardSpeed <= bottom(scoreBoard)
  153.        cor x(guard) + guardSpeed >= right(field)
  154.           setDir(guard, "W");
  155.        endif;
  156.     endCase
  157.   endSelect;
  158. }!!
  159.  
  160. /* Handle the expiring timer event. */
  161. Def  WM_TIMER(self | redo)
  162. { if playerLife <> 0
  163.   then moveGuards(self);
  164.    time := time + 1;
  165.    do(size(jewels),
  166.      {using(idx)
  167.      if age(jewels[idx]) <= 0
  168.      then jewels[idx] := moveJewel(self, jewels[idx]);
  169.      else rust(jewels[idx]);
  170.      endif;
  171.      });
  172.   else playerLife := -1;
  173.    redo := new(ErrorBox, ThePort,
  174.            "Would you like to play again?",
  175.            "Final Score: "+asString(score), 4);
  176.    select
  177.     case redo == IDYES
  178.     is playerLife := 5;
  179.        score := 0;
  180.        repaint(self);
  181.     endCase
  182.     case redo == IDNO
  183.     is Call KillTimer(hWnd, enemyMove);
  184.        close(self);
  185.     endCase
  186.    endSelect;
  187.   endif;
  188. }!!
  189.  
  190. /* Respond to MS-Window's mouse move message. 
  191.   Convert the mouse location data pointed to by 
  192.   lp to a Point object. */
  193. Def WM_MOUSEMOVE(self, wp, lp)
  194. { if inField(self, player)
  195.   then player := asPoint(lp);
  196.    do(size(jewels),
  197.    {using(idx | jewelSize)
  198.      jewelSize := size(jewels[idx]);
  199.      if x(player)+7 >= x(jewels[idx])-jewelSize
  200.      and x(player)-7 <= x(jewels[idx])+jewelSize
  201.      and y(player)+7 >= y(jewels[idx])-jewelSize
  202.      and y(player)-7 <= y(jewels[idx])+jewelSize
  203.      then score := score + 1;
  204.           showScore(self);
  205.           jewels[idx] := moveJewel(self, jewels[idx]);
  206.     endif;
  207.   });
  208.   endif;
  209. }!!
  210.  
  211. /* Set field boundries on resize. */
  212. Def  reSize(self, wP, lP)
  213. { field := clientRect(self);
  214.   repaint(self);
  215. }!!
  216.  
  217. /* Repaint items in window upon resizing. */
  218. /******************************************/
  219. Def  paint(self, hDC)
  220. { init(scoreBoard,right(field)-90,0,right(field),20);
  221.   draw(scoreBoard, hDC);
  222.   showScore(self);
  223.   Call TextOut(hDC, right(field)-85, 5, "Score:", 6); 
  224.   do(enemies,
  225.     {using(enemy) draw(enemy, hDC)
  226.     });
  227.   do(jewels,
  228.     {using(jewel) draw(jewel, hDC, field);
  229.     });
  230. }!!
  231.  
  232. /* Set up everything to play the game. */
  233. /***************************************/
  234. Def  init(self)
  235. { time := 0;                 /* Set initial time */
  236.   score := 0;                /* Set initial score */
  237.   playerLife := 5;           /* Lives remaining */
  238.   addAbout(self);            /* Set up About box */
  239.   field := clientRect(self); /* Set up playing field */
  240.   scoreBoard := new(Rect);   /* Set up scoreBoard */
  241.   enemies := new(Array,3);   /* Set up the guards */
  242.   do(size(enemies),
  243.     {using(enemy)
  244.      enemies[enemy] := new(Guard);
  245.     });
  246.   jewels := new(Array,3);    /* Set up the jewels */
  247.   do(size(jewels),
  248.     {using(idx)
  249.      jewels[idx] := new(Jewel); 
  250.     });
  251.                              /* Set up the timer */
  252.   enemyMove := Call SetTimer(hWnd, 0, 1, 0);
  253. }!!
  254.  
  255. 
  256.